/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThemXoaCapNhatNhanVienInternalFrame.java
 *
 * Created on Jul 1, 2010, 12:11:12 PM
 */

package GUI;


import java.util.ArrayList;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author May2
 */
public class ThemXoaCapNhatNhanVienInternalFrame extends javax.swing.JInternalFrame {

//    public void hienThiNhanVien(ArrayList<NhanVienPOJO> list){
//        //Tạo các cột trên Jtable
//        DefaultTableModel dtm = new DefaultTableModel();
//        dtm.addColumn("Mã Nhân Viên");
//        dtm.addColumn("Tên Nhân Viên");
//        dtm.addColumn("Ngày Sinh");
//        dtm.addColumn("Địa Chỉ");
//        dtm.addColumn("Email");
//        dtm.addColumn("Điện Thoại");
//        dtm.addColumn("Tên Phòng Ban");
//        //Tạo model
//        dtm.setRowCount(list.size());
//        for(int i=0; i<list.size(); i++){
//            dtm.setValueAt(list.get(i).getMaNhanVien(),i,0);
//            dtm.setValueAt(list.get(i).getTenNhanVien(),i,1);
//            dtm.setValueAt(list.get(i).getNgaySinh(),i,2);
//            dtm.setValueAt(list.get(i).getDiaChi(),i,3);
//            dtm.setValueAt(list.get(i).getEmail(),i,4);
//            dtm.setValueAt(list.get(i).getDienThoai(),i,5);
//            dtm.setValueAt(list.get(i).getTenPhongBan(),i,6);
//        }
//        this.jtbDanhSach.setModel(dtm);
//        this.jtbDanhSach.changeSelection(0, 0, false, false);
//    }
    public void hienThiNhanVienAll(){
//        ArrayList<NhanVienPOJO> list = NhanVienBUS.selectNhanVien();
//        this.hienThiNhanVien(list);
    }
    public void hienThiPhongBan(){
//        ArrayList<PhongBanPOJO> list = PhongBanBUS.selectPhongBanAll();
//        //Đọc dữ liệu
//        DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
//        for(int i=0; i<list.size(); i++){
//            //Đọc từng dòng
//            Vector ct = new Vector();
//            ct.add(list.get(i).getMaPhongBan());
//            ct.add(list.get(i).getTenPhongBan());
//            //Thêm một dòng vào model của JComboBox
//            dcbm.addElement(ct);
//        }
//        //Gán model cho JComboBox
//        this.jcbPhongBan.setModel(dcbm);
    }
    public void searchNhanVien(){
//        String tenNhanVien = this.jtfTenNhanVien.getText();
//        Vector vt = (Vector)this.jcbPhongBan.getSelectedItem();
//        int maPhongBan = (Integer)vt.get(0);
//        ArrayList<NhanVienPOJO> list = NhanVienBUS.searchNhanVien(maPhongBan, tenNhanVien);
//        this.hienThiNhanVien(list);
    }
    /** Creates new form ThemXoaCapNhatNhanVienInternalFrame */
    public ThemXoaCapNhatNhanVienInternalFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlbTenNhanVien = new javax.swing.JLabel();
        jtfTenNhanVien = new javax.swing.JTextField();
        jcbPhongBan = new javax.swing.JComboBox();
        jbtTim = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSach = new javax.swing.JTable();
        jbtThem = new javax.swing.JButton();
        jbtXoa = new javax.swing.JButton();
        jbtCapNhat = new javax.swing.JButton();
        jbtLen = new javax.swing.JButton();
        jbtXuong = new javax.swing.JButton();
        jbtThoat = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("THÊM - XÓA - CẬP NHẬT NHÂN VIÊN");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jlbTenNhanVien.setText("Tên Nhân Viên");

        jtfTenNhanVien.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfTenNhanVienCaretUpdate(evt);
            }
        });

        jcbPhongBan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbPhongBanActionPerformed(evt);
            }
        });

        jbtTim.setText("Tìm");
        jbtTim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtTimActionPerformed(evt);
            }
        });

        jtbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbDanhSach);

        jbtThem.setText("Thêm");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jbtXoa.setText("Xóa");
        jbtXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaActionPerformed(evt);
            }
        });

        jbtCapNhat.setText("Cập Nhật");
        jbtCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCapNhatActionPerformed(evt);
            }
        });

        jbtLen.setText("Lên");
        jbtLen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLenActionPerformed(evt);
            }
        });

        jbtXuong.setText("Xuống");
        jbtXuong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXuongActionPerformed(evt);
            }
        });

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jlbTenNhanVien)
                        .addGap(18, 18, 18)
                        .addComponent(jtfTenNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jcbPhongBan, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtTim, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbtCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtThem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtLen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtXuong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbTenNhanVien)
                    .addComponent(jtfTenNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbPhongBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtTim))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtThem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtXoa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtCapNhat)
                        .addGap(62, 62, 62)
                        .addComponent(jbtLen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtXuong)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtThoat))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.hienThiNhanVienAll();
        this.hienThiPhongBan();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jbtTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtTimActionPerformed
        // TODO add your handling code here:
        this.searchNhanVien();
    }//GEN-LAST:event_jbtTimActionPerformed

    private void jcbPhongBanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbPhongBanActionPerformed
        // TODO add your handling code here:
        this.searchNhanVien();
    }//GEN-LAST:event_jcbPhongBanActionPerformed

    private void jtfTenNhanVienCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfTenNhanVienCaretUpdate
        // TODO add your handling code here:
        this.searchNhanVien();
    }//GEN-LAST:event_jtfTenNhanVienCaretUpdate

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        int reply = JOptionPane.showConfirmDialog(this, "Bạn có muốn thoát", "Thông báo", WIDTH);
        if(reply == JOptionPane.OK_OPTION){
            this.dispose();
        }
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        // TODO add your handling code here:
        ThemNhanVienDialog dlg = new ThemNhanVienDialog(null, true);
        dlg.setVisible(true);
    }//GEN-LAST:event_jbtThemActionPerformed

    private void jbtXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaActionPerformed
        // TODO add your handling code here:
//        int dong = this.jtbDanhSach.getSelectedRow();
//        int maNhanVien = (Integer)this.jtbDanhSach.getValueAt(dong, 0);
//        int n = NhanVienBUS.deleteNhanVien(maNhanVien);
//        if(n==1){
//            JOptionPane.showMessageDialog(this, "Xóa thành công!!!");
//            this.hienThiNhanVienAll();
//        } else{
//            JOptionPane.showMessageDialog(this, "Xóa thất bại.");
//        }
        
    }//GEN-LAST:event_jbtXoaActionPerformed

    private void jbtCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCapNhatActionPerformed
        // TODO add your handling code here:
//        int dong = this.jtbDanhSach.getSelectedRow();
//        if(dong >= 0){
//            //Cung cấp dữ liệu cho dialog
//            CapNhatNhanVienDialog dialog = new CapNhatNhanVienDialog(null, true);
//            NhanVienPOJO nv = new NhanVienPOJO();
//            int maNhanVien = (Integer)this.jtbDanhSach.getValueAt(dong, 0);
//            String tenNhanVien = (String)this.jtbDanhSach.getValueAt(dong, 1);
//            String ngaySinh = (String)this.jtbDanhSach.getValueAt(dong, 2);
//            String diaChi = (String)this.jtbDanhSach.getValueAt(dong, 3);
//            String email = (String)this.jtbDanhSach.getValueAt(dong, 4);
//            String dienThoai = (String)this.jtbDanhSach.getValueAt(dong, 5);
//            String tenPhongBan = (String)this.jtbDanhSach.getValueAt(dong, 6);
//
//            nv.setMaNhanVien(maNhanVien);
//            nv.setTenNhanVien(tenNhanVien);
//            nv.setNgaySinh(ngaySinh);
//            nv.setDiaChi(diaChi);
//            nv.setEmail(email);
//            nv.setDienThoai(dienThoai);
//            nv.setTenPhongBan(tenPhongBan);
//            dialog.setNv(nv);
//            dialog.setVisible(true);
//
//            //Lấy dữ liệu được cập nhật từ dialog
//            nv.setMaNhanVien(dialog.getMaNhanVien());
//            nv.setTenNhanVien(dialog.getTenNhanVien());
//            nv.setNgaySinh(dialog.getNgaySinh());
//            nv.setDiaChi(dialog.getDiaChi());
//            nv.setEmail(dialog.getEmail());
//            nv.setDienThoai(dialog.getDienThoai());
//            nv.setMaPhongBan(dialog.getMaPhongBan());
//            NhanVienBUS.updateNhanVien(nv);
//            this.hienThiNhanVienAll();
//        }
    }//GEN-LAST:event_jbtCapNhatActionPerformed

    private void jbtLenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLenActionPerformed
        // TODO add your handling code here:
        int dong = this.jtbDanhSach.getSelectedRow();
        dong = dong - 1;
        if(dong >= 0){
            this.jtbDanhSach.changeSelection(dong, 0, false, false);
        } else {
            int dongCuoi = this.jtbDanhSach.getRowCount() - 1;
            this.jtbDanhSach.changeSelection(dongCuoi, 0, false, false);
        }
    }//GEN-LAST:event_jbtLenActionPerformed

    private void jbtXuongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXuongActionPerformed
        // TODO add your handling code here:
        int dong = this.jtbDanhSach.getSelectedRow();
        dong = dong + 1;
        if(dong < this.jtbDanhSach.getRowCount()){
            this.jtbDanhSach.changeSelection(dong, 0, false, false);
        } else {
            this.jtbDanhSach.changeSelection(0, 0, false, false);
        }
    }//GEN-LAST:event_jbtXuongActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtCapNhat;
    private javax.swing.JButton jbtLen;
    private javax.swing.JButton jbtThem;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JButton jbtTim;
    private javax.swing.JButton jbtXoa;
    private javax.swing.JButton jbtXuong;
    private javax.swing.JComboBox jcbPhongBan;
    private javax.swing.JLabel jlbTenNhanVien;
    private javax.swing.JTable jtbDanhSach;
    private javax.swing.JTextField jtfTenNhanVien;
    // End of variables declaration//GEN-END:variables

}
